﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Koonsoft.HYCRM.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Koonsoft.HYCRM.DAL
{
    public class LoginConditionDAL
    {
        private Database database = DatabaseFactory.CreateDatabase("KOONCRMDB");

        public List<LoginConditionEntity> GetEmplyeeContractByEmplyeeId(DateTime dateStart,DateTime dateEnd, int branchId)
        {
            var loginConditions = new List<LoginConditionEntity>();

            const string sql = "GetUserLoginCondition";

            using (var cmd = database.GetStoredProcCommand(sql))
            {
                database.AddInParameter(cmd, "@dateStart", DbType.DateTime, dateStart);
                database.AddInParameter(cmd, "@dateEnd", DbType.DateTime, dateEnd);
                database.AddInParameter(cmd, "@BranchId", DbType.Int32, branchId);

                var rd = (RefCountingDataReader)database.ExecuteReader(cmd);

                var sqlrd = (SqlDataReader)rd.InnerReader;

                while (sqlrd.Read())
                {
                    var item = new LoginConditionEntity
                    {
                        Name = sqlrd["name"].ToString(),
                        LoginName = sqlrd["LoginName"].ToString()
                    };

                    if (!Convert.IsDBNull(sqlrd["LoginTime"]))
                    {
                        item.LoginTime = Convert.ToDateTime(sqlrd["LoginTime"]);
                    }

                    loginConditions.Add(item);
                }
            }

            return loginConditions;
        }

    }
}
